package com.android.GernerateKeySet;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import android.util.Log;

public class PasswrodBasedCrypto{
	

	//private static byte[] recoveredText=null;
	//private static byte[] ciphertext = null;
	private byte[] eText=null;
	private byte[] dText=null;
	private PBEParameterSpec pbeParamSpec;
	public PasswrodBasedCrypto() {
		// Salt
		byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
				(byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };

		// Iteration count
		int count = 20;

		// Create PBE parameter set
		pbeParamSpec = new PBEParameterSpec(salt, count);

//		byte[] eText = encrytoText(pbeParamSpec,pwd);
//		byte[] dText = decrytoText(pbeParamSpec, eText,confirmpwd);
	}
	private static byte[] encrytoText(PBEParameterSpec pbeParamSpec, char[] pwd, byte[] pvKeyEncoded){

		PBEKeySpec pbeKeySpec;
		SecretKeyFactory keyFac;
		byte[] ciphertext = null;
		try {
			pbeKeySpec = new PBEKeySpec(pwd);
			keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

			// Create PBE Cipher
			Cipher encryptor = Cipher.getInstance("PBEWithMD5AndDES");
			Cipher decryptor = Cipher.getInstance("PBEWithMD5AndDES");

			// Initialize PBE Cipher with key and parameters
			encryptor.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
			decryptor.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

			// Our cleartext
			byte[] cleartext = pvKeyEncoded;
			Log.d("OriginalText","OriginalText : "+pvKeyEncoded.toString());
			// Encrypt the cleartext
			ciphertext = encryptor.doFinal(cleartext);
			String temp1 = new String(cleartext);
			System.out.println("원본 문자열 : "+ temp1);
			String temp2 = new String(ciphertext);
			System.out.println("암호화 문자열 : "+ temp2);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ciphertext;
	}
	
	private static byte[] decrytoText(PBEParameterSpec pbeParamSpec, byte[] ciphertext, char[] confirmpwd){
		
		PBEKeySpec pbeKeySpec;
		SecretKeyFactory keyFac;
		byte[] recoveredText = null;
		try {
			pbeKeySpec = new PBEKeySpec(confirmpwd);
			keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

			// Create PBE Cipher
			Cipher decryptor = Cipher.getInstance("PBEWithMD5AndDES");

			// Initialize PBE Cipher with key and parameters
			decryptor.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

			int recoveredTextLen = decryptor.getOutputSize (ciphertext.length);
			recoveredText = new byte[recoveredTextLen];
			recoveredText = decryptor.doFinal (ciphertext);

			String temp3 = new String(recoveredText);
			System.out.println("복호화 문자열 : "+ temp3);
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block

			System.out.println("2응?????");
			//e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			System.out.println("3응??");
			//e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block

			System.out.println("4응?????");
			//123e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block

			System.out.println("5응?????");
			//e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			System.out.println("6응?????");
			//e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			System.out.println("7응?????");
			//e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			System.out.println("fail");
			return null;
			//e.printStackTrace();
		} 
		return recoveredText;
	}
	public byte[] getEncryptionText(char[] pwd, byte[] encodedKey)
	{
		byte[] ciphertext = encrytoText(pbeParamSpec,pwd,encodedKey);
		return ciphertext;
	}
	
	public byte[] getDecryptionText(char[] confirmpwd, byte[] enCryptText)
	{
		
		byte[] recoveredText = decrytoText(pbeParamSpec,enCryptText,confirmpwd);
		if(recoveredText == null){
			return null;
		}
		else{
			return recoveredText;
		}
	}
}
